package org.example.binaryTree;

public class IsBalancedTree {


    /**
     * 是否为平衡二叉树
     *  得到的信息 左右子树是否为平衡二叉树 还有高度
     */
    public static class ReturnType{
        public boolean isBalanced;
        public int height;

        public ReturnType(boolean isBalanced, int height) {
            this.isBalanced = isBalanced;
            this.height = height;
        }
    }

    public static ReturnType process(Node x) {
        if(x == null) {
            return new ReturnType(true,0);
        }
        ReturnType leftData = process(x.left);
        ReturnType rightData = process(x.right);

        int height = Math.max(leftData.height, rightData.height) + 1;
        boolean isBalanced = leftData.isBalanced
                                        && rightData.isBalanced
                                        && Math.max(leftData.height, rightData.height) < 2;

        return new ReturnType(isBalanced,height);
    }


}
